﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;

namespace Business.Crypto
{
    public class SigningFiles
    {
        public static void Sing()
        {
            
            //step 1: create alg
            DSACryptoServiceProvider signer = new DSACryptoServiceProvider();

            //step 2: read file 
            FileStream file = new FileStream("c:\\config.sys", FileMode.Open, FileAccess.Read);
            BinaryReader reader = new BinaryReader(file);
            byte[] data = reader.ReadBytes((int)file.Length);

            //step 3: call sign meth + store sig
            byte[] signature = signer.SignData(data);

            //step 4: export public key
            string publicKey = signer.ToXmlString(false);

            Console.WriteLine("Signature: " + Convert.ToBase64String(signature));
            reader.Close();
            file.Close();

            
            //Verifing
            //step 1: Create alg
            DSACryptoServiceProvider verifier = new DSACryptoServiceProvider();

            //step 2: Import signature and public key
            verifier.FromXmlString(publicKey);

            //step 3: Store data to be verified
            FileStream file2 = new FileStream("c:\\config.sys", FileMode.Open, FileAccess.Read);
            BinaryReader reader2 = new BinaryReader(file2);
            byte[] data2 = reader2.ReadBytes((int)file2.Length);

            //step 4: verificare
            if (verifier.VerifyData(data2, signature))
            {
                Console.WriteLine("Signature verified");
            }
            else
            {
                Console.WriteLine("Signature is NOT verified");
            }

        }
       
        
        
        public static void Sing2()
        {
            //step 1: create alg
            //DSACryptoServiceProvider signer = new DSACryptoServiceProvider();
            RSACryptoServiceProvider signer = new RSACryptoServiceProvider();

            //step 2: read file 
            FileStream file = new FileStream("c:\\config.sys", FileMode.Open, FileAccess.Read);
            BinaryReader reader = new BinaryReader(file);
            byte[] data = reader.ReadBytes((int)file.Length);

            //step 3: call sign meth + store sig
            byte[] signature = signer.SignData(data, new SHA1CryptoServiceProvider());

            //step 4: export public key
            string publicKey = signer.ToXmlString(false);

            Console.WriteLine("Signature: " + Convert.ToBase64String(signature));
            reader.Close();
            file.Close();


            //Verifing
            //step 1: Create alg
            //DSACryptoServiceProvider verifier = new DSACryptoServiceProvider();
            RSACryptoServiceProvider verifier = new RSACryptoServiceProvider();

            //step 2: Import signature and public key
            verifier.FromXmlString(publicKey);

            //step 3: Store data to be verified
            FileStream file2 = new FileStream("c:\\config.sys", FileMode.Open, FileAccess.Read);
            BinaryReader reader2 = new BinaryReader(file2);
            byte[] data2 = reader2.ReadBytes((int)file2.Length);

            //step 4: verificare
            if (verifier.VerifyData(data2, new SHA1CryptoServiceProvider(), signature))
            {
                Console.WriteLine("Signature verified");
            }
            else
            {
                Console.WriteLine("Signature is NOT verified");
            }
        }

    
    }
}
